﻿using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using TelerikConnect.Models;
using TelerikConnect.Web.DBConnect;
using TelerikConnect.Web.Filters;
using TelerikConnect.Web.ViewModels;
using WebMatrix.WebData;

namespace TelerikConnect.Web.Controllers
{
    [Authorize(Roles="Admin")]
    public class AdminController : Controller
    {
        private readonly TelerikDb db = new TelerikDb();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Read([DataSourceRequest] DataSourceRequest request)
        {
            var data = db.UserProfiles.Select(
                u => new
            {
                About = u.About,
                AreaOfExpertise = u.AreaOfExpertise,
                Avatar = u.Avatar,
                Email = u.Email,
                FirstName = u.FirstName,
                LastName = u.LastName,
                Statement = u.Statement,
                UserId = u.UserId,
                UserName = u.UserName
            }
                );
            return Json(data.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [HttpPost]
        [Authorize(Roles = "Admin")]
        [InitializeSimpleMembership]
        public ActionResult Create([DataSourceRequest] DataSourceRequest request, UserProfile product)
        {
            if (product != null && ModelState.IsValid)
            {
                if (ModelState.IsValid)
                {
                    // Attempt to register the user
                    try
                    {
                        WebSecurity.CreateUserAndAccount(product.UserName, "password", new
                        {
                            FirstName = product.FirstName,
                            LastName = product.LastName,
                            Email = product.Email,
                            Avatar = RegisterViewModel.DefaultAvatar
                        });

                        db.Messages.Add(new MessageModel
                            {
                                Sender = db.UserProfiles.Single(u => u.UserName == User.Identity.Name),
                                Reciever = db.UserProfiles.Single(u => u.UserName == product.UserName),
                                SentTime = DateTime.Now,
                                Read = false,
                                Title = MessageModel.TITLE,
                                Content = MessageModel.CONTENT
                            });
                        db.SaveChanges();

                        return Content("Account created!");
                    }
                    catch (MembershipCreateUserException)
                    {
                        //ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                    }
                }

                // If we got this far, something failed, redisplay form
                return Content("Createion failed!");
            }

            return Json(new[] { product }.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Update([DataSourceRequest] DataSourceRequest request, UserProfile product)
        {
            if (product != null && ModelState.IsValid)
            {
                var target = db.UserProfiles.Single(u => u.UserName == product.UserName);
                if (target != null)
                {
                    target.About = product.About;
                    target.AreaOfExpertise = product.AreaOfExpertise;
                    target.Avatar = product.Avatar;
                    target.Email = product.Email;
                    target.FirstName = product.FirstName;
                    target.LastName = product.LastName;
                    target.Statement = product.Statement;
                    target.UserName = product.UserName;
                    db.SaveChanges();
                }
            }

            return Json(ModelState.ToDataSourceResult());
        }
    }
}